package com.waimai.mobile.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.waimai.mobile.R;
import com.waimai.mobile.api.entity.Order;
import com.waimai.mobile.api.entity.OrderStatus;
import com.waimai.mobile.misc.ImageLoader;
import com.waimai.mobile.ui.activity.CommentActivity_;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by lin on 2015/5/30.
 */

@EBean
public class OrderItemAdpater extends RecyclerView.Adapter<OrderItemAdpater.ViewHolder>{


    @RootContext
    public  Context context;

    public List<Order> orders;

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_list_order,viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int i) {
        viewHolder.orderStatusTv.setText(orders.get(i).getOrderstatus().getStatusName());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String orderTimeStr = format.format(orders.get(i).getOrderTime());
        viewHolder.orderTimeTv.setText(orderTimeStr);
        viewHolder.restNameTv.setText(orders.get(i).getRestaurant().getRestaurantName());
        viewHolder.totalPriceTv.setText(orders.get(i).getTotalPrice() + "");
        if(orders.get(i).getOrderstatus().getStatusId() == OrderStatus.STATUS_NO_COMMENT){
            viewHolder.containerRL.setVisibility(View.VISIBLE);
            viewHolder.toOrderCommentBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, CommentActivity_.class);
                    intent.putExtra("order", orders.get(i));
                    context.startActivity(intent);
                }
            });
        }else{
            viewHolder.containerRL.setVisibility(View.GONE);
        }

        ImageLoader.load(orders.get(i).getRestaurant().getPhoto(), viewHolder.restPhotoIv);

    }


    @Override
    public int getItemCount() {
        return orders.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView restPhotoIv;
        public TextView restNameTv;
        public TextView orderTimeTv;
        public TextView totalPriceTv;
        public TextView orderStatusTv;
        public RelativeLayout containerRL;
        public Button toOrderCommentBtn;

        public View view;


        public ViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            restPhotoIv = (ImageView) itemView.findViewById(R.id.restPhotoIv);
            orderTimeTv = (TextView) itemView.findViewById(R.id.orderTimeTv);
            restNameTv = (TextView) itemView.findViewById(R.id.restNameTv);
            totalPriceTv = (TextView) itemView.findViewById(R.id.totalPriceTv);
            orderStatusTv = (TextView) itemView.findViewById(R.id.orderStatusTv);
            containerRL = (RelativeLayout) itemView.findViewById(R.id.containerRL);
            toOrderCommentBtn = (Button) itemView.findViewById(R.id.toOrderCommentBtn);
        }
    }
}
